package com.health.es.generate;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReflectUtil;
import com.health.es.entity.data.*;
import com.health.es.entity.data.tool.OpcInfo;
import com.health.es.temp.EsHelper;
import lombok.SneakyThrows;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author nqlz
 */
@Component
public class OpcChildGenerate<T> extends GenerateExecute<T> {

    @Autowired
    RestHighLevelClient restHighLevelClient;
    private final Map<Class<?>, List<Class<? extends OpcInfo>>> mainKeyMap = new HashMap<>();

    @Override
    @PostConstruct
    protected void initClasses() {
        addClasses(EsOpcPrescription.class, EsIpDoctorAdvice.class, EsTestApply.class, EsCkeckApply.class, EsSurgeryApply.class);
        mainKeyMap.put(EsOpcPrescription.class, Collections.singletonList(EsOpcSeeDoctor.class));
        mainKeyMap.put(EsIpDoctorAdvice.class, Collections.singletonList(EsIpRecord.class));
        List<Class<? extends OpcInfo>> usualList = Arrays.asList(EsOpcSeeDoctor.class, EsIpRecord.class);
        mainKeyMap.put(EsTestApply.class, usualList);
        mainKeyMap.put(EsCkeckApply.class, usualList);
        mainKeyMap.put(EsSurgeryApply.class, usualList);
    }

    @Override
    @SneakyThrows
    public void run(Collection<?> objects, Class<T> tClass) {
        List<Class<? extends OpcInfo>> classes = mainKeyMap.get(tClass);
        List<T> tList = objects.parallelStream()
                .map(t -> executeEntity(t, tClass))
                .peek(t -> tellMainOpc(t, tClass, classes))
                .collect(Collectors.toList());
        EsHelper.operations.save(tList);
    }


    /**
     * 告诉主表opc
     * 主表信息先入库情况
     * 主先，查询 并修改
     * 子先，无需关注
     *
     * @param t           t
     * @param mainClasses 主要类
     */
    private void tellMainOpc(T t, Class<T> tClass, List<Class<? extends OpcInfo>> mainClasses) {
        //获取关联的主键值
        String hospCodeTreatment = ReflectUtil.getFieldValue(t, "hospCodeTreatment").toString();
        MatchPhraseQueryBuilder builder = QueryBuilders.matchPhraseQuery("hospCodeTreatment", hospCodeTreatment);
        Optional<? extends OpcInfo> first = mainClasses.stream().map(i -> EsHelper.searchOne(i, builder)).filter(Objects::nonNull).findFirst();
        first.ifPresent(i -> {
            //修改单独 关联值
            if (!i.hasOpcInfo(tClass)) {
                if (tClass.equals(EsOpcPrescription.class)) {
                    EsOpcPrescription prescription = Convert.convert(EsOpcPrescription.class, t);
                    if (prescription.getDetailCount() < 1) {
                        return;
                    }
                }
                i.setOpcInfo(tClass);
                EsHelper.operations.save(i);
            }
        });
    }

}
